package cn.edu360.service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

import cn.edu360.pojo.User;



@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Override
	public boolean sendMsg(String countryCode, String phoneNum) {
		
		boolean flag = true;
		// 调用腾讯云的短信API
		int appid = Integer.parseInt(stringRedisTemplate.opsForValue().get("appid"));
		String appkey = stringRedisTemplate.opsForValue().get("appkey");
		//生成一个随机的数字4位
		String code = (int)((Math.random() *9 + 1) * 1000) + "";
		SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
		try {
			//向对应手机号的用户发送短信
			ssender.send(0, countryCode, phoneNum, "您的登录验证码为" + code, "", "");
			//将发送的手机号作为key,验证码作为value保存到Redis中
			stringRedisTemplate.opsForValue().set(phoneNum, code,300,TimeUnit.SECONDS);
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}
	    
		
		
		
		
		return flag;
	}

	@Override
	public boolean verify(String phoneNum, String verifyCode) {
		boolean flag = false;
		// TODO Auto-generated method stub
		String code = stringRedisTemplate.opsForValue().get(phoneNum);
		if(code != null && code.equals(verifyCode)) {
			flag = true;
		}
		return flag;
	}

	@Override
	public void register(User user) {
		
		//调用mongodb的dao,将用户数据保存起来
		mongoTemplate.insert(user);
		
		}

	@Override
	public void update(User user) {
		//如果对应_id的数据不存在，就插入；存在则更新
		//根据用户的手机号对数据进行更新
		//mongoTemplate.insert(user);
		//更新用户对应的属性
		Update update = new Update();
		if(user.getDeposite() != null) {
		 update.set("deposite", user.getDeposite());
		}
		if(user.getStatus() != null) {
			update.set("status",user.getStatus());
		}
		if(user.getName() != null) {
			update.set("name",user.getName());
		}
		if(user.getIdNum() != null) {
			update.set("idNum",user.getIdNum());
		}
		mongoTemplate.updateFirst(new Query(Criteria.where("phoneNum").is(user.getPhoneNum())), update,
				 User.class);
		
	}
	
}
